﻿--[[
   @brief    social network control
   @author   rickonzhang
   @version  1.0
   @date     2015-06-23
--]]

social_network_control = 
{
    is_im_inited = false;
    is_main_wnd_inited = false;
    common_topic_js_info = nil;
    add_friend_with_face_url = 1;
    game_config_info_ = {};   -- "key":str_game_id, "value": {"game_name","game_icon_url"}

    init = function ()
        base_utility.log("[social_network_control] init");
        ierd_tgp.tpf_ui.Require("social\\model.lua");
        ierd_tgp.tpf_ui.Require("social\\pwg_social\\control.lua");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        -- tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_IM_INITED, "social_network_control.on_im_inited");

        comm_center.reg_service('i_service_webutl_get_tgp_version', social_network_control.service_get_tgp_version);
        comm_center.reg_service('i_service_is_game_supported', social_network_control.service_is_game_supported);
        comm_center.reg_service("i_service_get_my_setting", social_network_control.handle_get_my_setting);
        comm_center.reg_service("i_service_get_all_setting", social_network_control.handle_get_all_setting);

        comm_center.listen_broadcast('i_broadcast_on_get_social_game_config_info',
            social_network_control.broadcast_on_get_social_game_config_info);
        comm_center.listen_broadcast("i_broadcast_on_setting_change", social_network_control.on_setting_change);
        comm_center.listen_broadcast("i_service_call_platform_get_game_config_info",
            social_network_control.service_call_platform_get_game_config_info);
        comm_center.listen_broadcast("i_broadcast_user_set_storage_update", social_network_control.on_user_set_storage_update);
    end;

    load_layout_finish = function(event_id, json_str)
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl then
            local uilib = ui_utility.uilib();
            local custom_id = 0;
            local ret = FALSE;
            ret, custom_id = uilib:GetCustomIDbyControlID(param_tbl["id"], custom_id);
            if CID_MAIN_WINDOW == custom_id then
                event_center.remove_listener(layout_event.RESTORE_LAYOUT_FINISH, "social_network_control.load_layout_finish");
                social_network_control.is_main_wnd_inited = true;
                if social_network_control.is_im_inited == true then
                    social_network_control.init_social_network();
                end
            end
        end
    end;

    on_im_inited = function(event_id, json_str)
        social_network_control.is_im_inited = true;
        base_utility.log("[social_network_control] on_im_inited complete"..json_str);
        login_model.im_login_infomation = base_utility.json_to_table(json_str);

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_IM_INITED, "social_network_control.on_im_inited");
        if social_network_control.is_main_wnd_inited == true then
            social_network_control.init_social_network();
        end

        --下载 http://cdn.tgp.qq.com/bin_res/ossjson/common_topic_info.js
        social_network_control.download_common_topic_info();
        social_network_control.get_message_box_cfg();
        social_network_control.get_social_game_config_info();
        -- 获取和好友wegame或者游戏上线相关的设置
        social_network_control.get_user_setting();
    end;

    init_social_network = function()
        --event_center.send_event(group_event.SHOW_FRIEND_LIST, "");
        --event_center.send_event(group_event.SHOW_GROUP_LIST, "");
    end;

    on_open_personal_center = function(event_id, json_str)
        base_utility.log("[login_ui] on_open_personal_center"..json_str);
        local param_in = base_utility.json_to_table(json_str);
        local param = {};
        param.friend_id = param_in.tgp_id;
        event_center.send_event(group_event.SHOW_PERSONAL_CENTER,base_utility.table_to_json(param));
    end;

    get_my_remark_info = function()
        local from = {};
        from["s"] = tonumber(login_model.im_login_infomation["sex"]);
        if social_network_control.add_friend_with_face_url == 1 then
            from["f"] = login_model.im_login_infomation["url"];
        end
        from["n"] = login_model.im_login_infomation["name"];
        from["sd"] = login_model.im_login_infomation["show_id"];  -- show_id
        return from;
    end;

    --------------------
    open_qq_friend_panel = function(param_in)
        local param = {};
        event_center.send_event(group_event.SHOW_QQ_FRIEND_DIALOG, base_utility.table_to_json(param));
    end;

    open_game_friend_panel = function(param_in)
        local param = {};
        event_center.send_event(group_event.SHOW_GAME_FRIEND_DIALOG, base_utility.table_to_json(param));
    end;

    open_search_pannel = function(param_in)
        local param = {};
        if param_in.tab == "group_search" then
            param = {url = "/sociality/search.html?tab=0"};
        elseif param_in.tab == "friend_search" then
            param = {url = "/sociality/search.html?tab=1"};
        else
            param = {url = "/sociality/search.html?tab=0"};
        end
        event_center.send_event(group_event.SHOW_GROUP_SEARCH_DIALOG, base_utility.table_to_json(param));
    end;

    add_friend = function(param_in)
        if param_in ~= nil then
            
            local ext_info = {};
            ext_info["type"] = param_in.relationship

            local from = {};
            if login_model.im_login_infomation then
                from = social_network_control.get_my_remark_info();
            end
            ext_info["f"] = from;

            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_ADD_FRIEND, "social_network_control.add_friend_rsp");

            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social_network:add_friend(param_in.friend_id, base_utility.table_to_json(ext_info));    
        end
    end;

    add_friend_rsp = function(event_id, json_str)  
        base_utility.log("[social_network_control] add_friend_rsp: "..json_str);
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_ADD_FRIEND, "social_network_control.add_friend_rsp");
        comm_center.send_broadcast("i_broadcast_social_add_friend", base_utility.json_to_table(json_str));
    end;

    web_call_social_event = function(event_id, json_str)
        local param = base_utility.json_to_table(json_str);
        local func_name = param.func_name;
        if func_name == "social_update_msg_event" then
            social_network_control.social_update_msg_event(param.json);
        elseif func_name == "jc_set_social_tip" then
            social_network_control.social_show_friends_info_tips_wnd(param.json);
        elseif func_name == "jc_close_social_ad_tips" then
            social_network_control.close_social_ad_tips(param.json);
        end
    end;

    social_update_msg_event = function(json_str)
        comm_center.call_service('i_service_social_network_update_tips_msg_info', {});
    end;

    social_show_friends_info_tips_wnd = function(json_str)
        local param_in = base_utility.json_to_table(json_str);
        event_center.send_event(social_event.OPEN_FRIENDS_INFO_TIPS_WND, base_utility.table_to_json(param_in.data));
    end;

    close_social_ad_tips = function(json_str)
        local param = base_utility.json_to_table(json_str);
        event_center.send_event(social_event.CLOSE_SOCIAL_AD_TIPS, base_utility.table_to_json({ad_id = param.data.ad_id}));
    end;

    is_my_friend = function(data)
        local is_my_friend = false;
        if friend_list_model.get_friend_info(data.friend_id) ~= nil then
            is_my_friend = true;
        end 
        return {is_my_friend = is_my_friend}
    end;

    get_my_info = function(data)
        local user_game_info = ierd_tgp.users_info.User_game_info:new();
        local users_info_adapter =
            base_utility.get_component_adapter("IUsers_info_module",
                "ierd_tgp.users_info.GetUsersInfoModuleAdapter();");
        users_info_adapter:get_self_game_info(user_game_info);

        if login_model.im_login_infomation == nil then
            return {};
        end

        local param_table = {};
        param_table["id"] = login_model.im_login_infomation.friend_id;
        param_table["my_nick"] = login_model.im_login_infomation.name;
        param_table["head_url"] = login_model.im_login_infomation.url;
        param_table["remark"] = login_model.im_login_infomation.remark;
        param_table["level"] = login_model.im_login_infomation.level;
        param_table["sex"] = login_model.im_login_infomation.sex;    
        param_table["exp"] = login_model.im_login_infomation.exp;
        param_table["reg_time"] = login_model.im_login_infomation.reg_time; 
        param_table["last_login_time"] = login_model.im_login_infomation.last_login_time;
        param_table["status"] = login_model.im_login_infomation.status;      
        param_table["show_id"] = login_model.im_login_infomation.show_id;    
        param_table["uin"] = user_game_info.QQid;
        param_table["zone_name"] = talk_dialog_control.get_my_game_zone_info(26);

        base_utility.log(string.format("my info str = %s",base_utility.table_to_json(param_table)));

        user_game_info:delete();
        return param_table;
    end;

    open_person_center = function(data)
        local param_table = {friend_id = data.friend_id};
        if data.tab_index ~= nil then
            local ext_param = "tab_index="..tostring(data.tab_index);
            param_table.param = ext_param;
        end
        event_center.send_event(group_event.SHOW_PERSONAL_CENTER, base_utility.table_to_json(param_table));
        return {};
    end;

    begin_talk_with_friend = function(data)
        local friend_info = friend_list_model.get_friend_info(data.friend_id);
        if friend_info ~= nil then
            local table_param = {};
            local friends = {};

            local info = {};
            info.id = data.friend_id;
            info.is_new = false;
            info.time = friend_list_model.get_last_msg_time(data.friend_id);
            table.insert(friends, info);

            table_param["friends"] = friends;
            event_center.send_event(group_event.SHOW_TALK_DIALOG, base_utility.table_to_json(table_param));
            return {result = true};
        else 
            base_utility.log("[social_network_control] begin_talk_with_friend: is not friend:"..tostring(data.friend_id));
            return {result = false};
        end
    end;

    begin_talk_with_group = function(data)
        local group_info = group_list_model.get_group_info(data.group_id);
        if group_info ~= nil then
            local table_param = {};
            local groups = {};

            local info = {};
            info.id = data.group_id;
            info.is_new = false;
            info.time = group_list_model.get_last_msg_time(info.id);
            info.group_type = group_list_model.get_group_type(info.id);
            table.insert(groups, info);

            table_param["groups"] = groups;
            event_center.send_event(group_event.SHOW_TALK_DIALOG, base_utility.table_to_json(table_param));
            return {result = true};
        else 
            base_utility.log("[social_network_control] begin_talk_with_group: is not friend:"..tostring(data.group_id));
            return {result = false};
        end
    end;

    pallas_to_social = function(data)
        if data.cmd == "PALLAS_TO_SOCIAL" then
            local param = base_utility.json_to_table(data.param);
            if param ~= nil then
                if param.type == "tgp_social" then
                    if param.sub_type == "open_talk_dlg" then
                        local id = tostring(param.id);
                        if param.id_type == 0 then  --friend
                            comm_center.call_service(social_services.i_service_social_begin_talk_with_friend, {friend_id = id});
                        elseif param.id_type == 1 then -- group
                            comm_center.call_service(social_services.i_service_social_begin_talk_with_group, {group_id = id});
                        end 
                    elseif param.sub_type == "add_friend" then
                        local friend_id = tostring(param.id);
                        local relationship = 3;
                        local r = tonumber(param.relationship);
                        if r > 0 and r <= 3 then
                            relationship = r;
                        end
                        comm_center.call_service(social_services.i_service_social_add_friend, {friend_id = friend_id, relationship = relationship});
                    end
                end
            end
        end
    end;

    get_message_box_cfg = function()
    end;

    process_get_message_box_cfg = function(data_in)
        social_network_control.download_msg_box_cfg_task_id = nil;
        if data_in.result == 200 then
            local param = base_utility.json_to_table(data_in.rsp);
            if param.result == 0 then
                for key, item in pairs(param.cfg_list) do
                    if item["cfg_name"] == "have_face_url" then
                        social_network_control.add_friend_with_face_url = tonumber(item["cfg_value"]);
                        break;
                    end
                end
            end
        end
    end;

    get_social_game_config_info = function()
        comm_center.call_service("i_service_get_social_game_config_info", {});
    end;

    broadcast_on_get_social_game_config_info = function (data_in) -- pb协议拉取回来的
        if data_in ~= nil and data_in.json_str ~= nil and data_in.json_str ~= "" then
            local tbl_data = base_utility.json_to_table(data_in.json_str);
            if tbl_data ~= nil and type(tbl_data) == "table" then
                for index, game_info in ipairs(tbl_data) do
                    if game_info ~= nil and type(game_info) == "table" then
                        local game_id = tostring(game_info.game_id);
                        social_network_control.game_config_info_[game_id] = {
                            game_name = game_info.game_name;
                            game_icon_url = game_info.banner_icon_url;
                        };
                    end
                end
            end
        end
    end;

    service_is_game_supported = function(data_in)
        local support = false;
        if data_in.game_id > 2000000 then
            local result = social_network_control.game_config_info_[tostring(data_in.game_id)];
            if nil ~= result then
                support = true;
            else
                -- 从游戏基本信息里获取一下
                local base_info = banner_model.get_game_base_info(data_in.game_id);
                if base_info ~= nil then
                    support = true;
                end    
            end
        else
            support = true;
        end
        return {game_id = data_in.game_id, is_supported = support};
    end;

    --下载 http://cdn.tgp.qq.com/bin_res/ossjson/common_topic_info.js
    download_common_topic_info = function()
    end;

    process_download_common_topic_info = function(data_in)
        social_network_control.download_js_http_task_id = nil;
        if data_in.result == 200 then
            local b_pos = string.find(data_in.rsp, "{", 1);
            local len = string.len(data_in.rsp);
            if b_pos ~= nil and len ~= nil and len > b_pos then
                social_network_control.common_topic_js_info = string.sub(data_in.rsp, b_pos, len - 1);
                base_utility.log("[social_network_control] js info:"..social_network_control.common_topic_js_info);
            end
        else
            base_utility.log("[social_network_control] http_task_rsp error:"..base_utility.table_to_json(data_in));
        end
    end;

    http_task_rsp = function(data_in)
        if social_network_control.download_js_http_task_id == data_in.task_id then
            social_network_control.process_download_common_topic_info(data_in);
        elseif social_network_control.download_msg_box_cfg_task_id == data_in.task_id then
            social_network_control.process_get_message_box_cfg(data_in);
        end
    end;

    get_common_topic_js_info = function(data_in)
        return {js_info = social_network_control.common_topic_js_info};
    end;

    open_game_detial_web_page = function(data_in)
        if data_in ~= nil and data_in.game_id ~= nil then
            local param = {};
            param["type"] = "mmo_game";
            param["sub_page"] = "GAME_DETAIL"; 
            param["game_id"] = tostring(data_in.game_id); 
            web_utility.open_link_convert(base_utility.table_to_json(param));
        end
    end;

    service_report_social_network_quality = function (data_in)
        local cmd = data_in.cmd;
        local subcmd = data_in.subcmd;
        local timeout = data_in.timeout;
        local errorcode = data_in.errorcode;
        local time_cost = data_in.time_cost;
        tqos_mgr.report_social_newwork_package_quality(cmd, subcmd, timeout, errorcode, time_cost);
    end;

    get_friend_playing_game = function(data_in)
        local result = false;
        local game_id = 0;
        local game_status = 0;

        if friend_list_model ~= nil and data_in.friend_id ~= nil then
            game_status, game_id = friend_list_model.get_friend_status(tostring(data_in.friend_id));
            if game_id ~= nil then
                result = true;
            end
        end;
        return {result = result, game_id = game_id, game_status = game_status};
    end;

    open_game_web_page = function(data_in)
        if data_in.game_id ~= nil and data_in.page ~= nil then
            if data_in.page == 0 then
                local out = comm_center.call_service('service_game_library_is_game_in_list', {game_id = data_in.game_id});
                if out.is_installed == false then
                    comm_center.call_service('i_service_social_open_game_detail_web_page', {game_id = tonumber(data_in.game_id)});
                else
                    local param = {};
                    param["type"] = "landing";
                    param["game_id"] = tostring(data_in.game_id);
                    web_utility.open_link_convert(base_utility.table_to_json(param));
                end

            elseif data_in.page == 1 then --打开游戏详情
                comm_center.call_service('i_service_social_open_game_detail_web_page', {game_id = tonumber(data_in.game_id)});

            elseif data_in.page == 2 then --打开助手
                local param = {};
                param["type"] = "landing";
                param["game_id"] = tostring(data_in.game_id);
                web_utility.open_link_convert(base_utility.table_to_json(param));
            elseif data_in.page == 3 then  -- 打开主web的个人中心页
                local param = {}
                param.type ="profile"
                param.uin = data_in.uin
                web_utility.open_link_convert(base_utility.table_to_json(param));
            end
        end
    end;

    get_all_game_id_list = function(data_in)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");

        local base_info_id_list = ierd_tgp.game_library.GameBaseInfoIDList:new_local();
        if nil == base_info_id_list then
            return;
        end;
        
        game_lib:GetAllGameBaseInfoIDList(base_info_id_list);

        local all_game_list = {};
        for i = 0, (base_info_id_list.count - 1) do
            local game_id = base_info_id_list.game_id_list[i];
            table.insert(all_game_list, game_id);
        end;

        return all_game_list;
    end;

    service_get_tgp_version = function(data_in)
        local ver = 0;
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= game_lib then
            ver = game_lib:get_tgp_version_num();
        end

        local data_out = {version=ver};
        return data_out;
    end;

    on_user_set_storage_update = function()
        social_network_control.get_user_setting();
    end;

    get_user_setting = function ()
        base_utility.log("[social_network_control][get_user_setting]", true);
        local url = LoadStr("GET_USER_SETTING_URL");
        --url = web_utility.make_login_status_url(url, wegame_mainframe_model.self_uin);
        base_utility.log("[social_network_control][get_user_setting]url:" .. url, true);
        base_utility.curl_request_async (url,
            "social_network_control.handle_get_user_setting", true, nil, true);
    end;

    handle_get_user_setting = function (data)
        local result_tb = base_utility.json_to_table(data);
        base_utility.log("[social_network_control][handle_get_user_setting]data:" .. data, true);
        if nil == result_tb then
            return;
        end

        if 0 == result_tb.Ret then
            social_network_model.user_online_setting = base_utility.json_to_table(result_tb.ItemValue);
            comm_center.send_broadcast("i_broadcast_user_setting_update", {});
        end
    end;

    handle_get_my_setting = function (data)
        if nil == data then
            return;
        end

        if nil ~= social_network_model.user_online_setting then
            data.value = social_network_model.user_online_setting[data.key];
        end        

        if data.key == "friend_game_online_notify" then
            if nil == data.value or nil == social_network_model.user_online_setting then
                data.value = 1; -- 这个设置项默认值是1
            end
        end
        return data;
    end;

    on_setting_change = function (data)
        base_utility.log("[social_network_control][on_setting_change]get!");
        if nil == data then
            return;
        end
        
        if nil ~= social_network_model.user_online_setting then
            for k, v in pairs(data) do
                social_network_model.user_online_setting[k] = v;
            end
        end
    end;

    handle_get_all_setting = function (data)
        if nil == data then
            return;
        end
        data["all_setting"] = social_network_model.user_online_setting
        return data;
    end;

    service_call_platform_get_game_config_info = function()
        local json_data = base_utility.table_to_json(social_network_control.game_config_info_);
        data_out = {};
        data_out.json_data = json_data;
        comm_center.send_broadcast('i_broadcast_to_app_on_game_config_info', data_out);
    end;

    on_self_info_update = function(data)
        if nil == data or type(data) ~= 'table' then
            base_utility.log("[social_network_control][on_self_info_update]invalid data", true);
            return;
        end
        if nil ~= login_model.im_login_infomation and
            type(login_model.im_login_infomation) == 'table' then
            base_utility.log("[social_network_control][on_self_info_update] current: ".. 
                              base_utility.table_to_json(login_model.im_login_infomation));
        end
        if nil == login_model.im_login_infomation then
            login_model.im_login_infomation = {};
        end
        base_utility.log("[social_network_control][on_self_info_update] new: " ..
                          base_utility.table_to_json(data));
        if nil ~= data.head_url and "" ~= data.head_url then
            login_model.im_login_infomation.url = data.head_url;
        end
        if nil ~= data.nick_name and "" ~= data.nick_name then
            login_model.im_login_infomation.name = data.nick_name;
        end
    end;
}

social_network_control.init();